package com.okaixz.client.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.okaixz.client.mapper.UserFeedbackMapper;
import com.okaixz.client.model.domain.User;
import com.okaixz.client.model.domain.UserFeedback;
import com.okaixz.client.service.IUserFeedbackService;
import com.okaixz.client.service.IUserService;
import com.okaixz.common.client.exception.BusinessException;
import com.okaixz.common.client.exception.CommonErrorCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cdz
 * @since 2025-08-20
 */
@Service
public class UserFeedbackServiceImpl extends ServiceImpl<UserFeedbackMapper, UserFeedback> implements IUserFeedbackService {

    @Autowired
    private IUserService userService;

    @Override
    public boolean addFeedback(UserFeedback userFeedback) {
        String userId = userFeedback.getUserId();
        String content = userFeedback.getContent();

        if(userId==null||"".equals(userId)){
            throw new BusinessException(CommonErrorCode.USER_NOT_EXIST);
        }
        if(content==null||"".equals(content)){
            throw new BusinessException(CommonErrorCode.PARAM_ERROR);
        }

        User user = userService.getUserFormRedisOrDb(userId);
        if (user == null) {
            throw new BusinessException(CommonErrorCode.USER_NOT_EXIST);
        }


        String clientName = user.getClientName();

        userFeedback.setClientName(clientName);
        userFeedback.setCreatTime(LocalDateTime.now());

        return save(userFeedback);

    }
}
